[Swift] UITableView

Table View vs Table View Cell

Table View

Displays data in a list

  • Coordinates with a data source and delegate to display a scrollable list of rows. Each row in a table view is a UITableViewCell object.
  • The rows can be grouped into sections, and the sections can optionally have headers and footers.
  • The user can edit a table by inserting, deleting, and reordering table cells.

Table View Cell

Single row of a table view

  • Defines the attributes and behavior of cells in a table view. You can set a table cell’s selected-state appearance, support editing functionality, display accessory views (such as a switch control), and specify background appearance and content indentation.


UITableViewDataSource

requires 2 delegate methods

1

  • 첫 번째에서는 디스플레이할 총 셀 갯수를 리턴 (Int)
  • 두 번째에서는 각 index에서 디스플레이할 셀을 리턴 (UITableViewCell)

    • 그래서 사용하는 method가 dequeueReusableCell

      Returns a reusable table-view cell object for the specified reuse identifier and adds it to the table.


Customizing Cells in a UITableView

  • Creating .xib file

    create Cocoa Touch Class file under Views folder

    2

  • Register Customized Cell

    • ChatViewController.swiftviewDidLoad() 안에서

      tableView.register(UINib(nibName: K.cellNibName, bundle: nil), forCellReuseIdentifier: K.cellIdentifier)
    • extend한 코드에서 as! MessageCell 추가

      extension ChatViewController: UITableViewDataSource {
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return messages.count
          }
          
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath)
                  as! MessageCell
              cell.label.text = messages[indexPath.row].body
              return cell
          }
      }


ABOUT ME
I write codes and words.
제가 궁금하다면 ABOUT ME 버튼을 눌러보세요!

GitHubLinkedIn